package com.ruoyi.central.publisher.subscription.art.process.run.event;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.central.domain.TopCentralArtProcessRun;
import com.ruoyi.central.domain.TopCentralProcessStepRun;
import com.ruoyi.central.publisher.event.ArtProcessRunEvent;
import com.ruoyi.central.publisher.event.ProcessStepRunEvent;
import com.ruoyi.central.service.ITopCentralProcessStepRunService;
import com.ruoyi.common.utils.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Classname ProcessStepSubscriptionArtProcessRunEvent
 * @Author: sunluquan
 * @Date: 2022/5/24 18:15
 * @Description TODO
 * 订阅相关 工艺执行对象 发布的事件
 */
@Component
public class ProcessStepSubscriptionArtProcessRunEvent {

    @Autowired
    private ITopCentralProcessStepRunService processStepRunService;

    /**
     *
     * @param event
     * @return
     */
    @EventListener(value = ArtProcessRunEvent.class,condition = "#event.getSignEnum().name()=='DELETE'")
    public ProcessStepRunEvent deleteEvent(ArtProcessRunEvent event){

        List<Integer> artProcessRunIds = CollectionUtils.mapToList(event.getSources(), TopCentralArtProcessRun::getId);

        LambdaQueryWrapper<TopCentralProcessStepRun> queryWrapper = processStepRunService.lambdaQuery();

        queryWrapper.select(TopCentralProcessStepRun::getId);

        if(artProcessRunIds.size()==1){
            queryWrapper.eq(TopCentralProcessStepRun::getArtProcessRunId,CollectionUtils.getFirst(artProcessRunIds));
        }else{
            queryWrapper.in(TopCentralProcessStepRun::getArtProcessRunId,artProcessRunIds);
        }

        List<TopCentralProcessStepRun> processStepRunsOfDelete = processStepRunService.selectSimpleList(queryWrapper);

        List<Integer> processStepRunIdsOfDelete = CollectionUtils.mapToList(processStepRunsOfDelete, TopCentralProcessStepRun::getId);

        processStepRunService.getBaseMapper().deleteBatchIds(processStepRunIdsOfDelete);

        return new ProcessStepRunEvent(processStepRunsOfDelete,event.getSignEnum(),true);
    }

}
